﻿// Decompiled with JetBrains decompiler
// Type: Netick.ClientConnection
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;

#nullable disable
namespace Netick;

public sealed class ClientConnection(short clientIndex, NetickEngine engine) : NetworkConnection(clientIndex, engine)
{
  internal override int WritePacketsSecondPass(SerializationCache cache)
  {
    this._writeBuffer.AddInt(this.DCTick.Value);
    this._writeBuffer.AddInt(this.LastReceivedFullSnapshotTick_State.Value);
    this._writeBuffer.AddBool(this.Engine._cliSim.SimulationReady);
    this._writeBuffer.AddBool(this.Engine.Timer.HeavyPktLoss);
    Simulation.WriteInputs(this.Engine, this._writeBuffer, this.MaxOutgoingDataPerTickInBytes);
    this._sentUpdatesTimes[this.Engine._snapshotTick % this.Engine._config.MaxSnapshots] = this.Engine._timer.Time;
    return this._writeBuffer.Length();
  }

  internal override unsafe void ReadPayload(
    BitBuffer reader,
    Tick snapshotTick,
    Tick ackSnapshotTick,
    Tick lastSnapshotTick)
  {
    ClientSimulation clientSimulation = this.Engine.ClientSimulation;
    if (!reader.ReadBool())
    {
      clientSimulation.IsLastTickFull = false;
    }
    else
    {
      int num1 = reader.ReadInt();
      Tick simulationTick = reader.ReadTick();
      Tick tick = reader.ReadTick();
      bool didWriteAll1 = reader.ReadBool();
      bool flag1 = reader.ReadBool();
      bool simReady = reader.ReadBool();
      int num2 = reader.ReadInt();
      int num3 = reader.ReadInt();
      uint num4 = reader.ReadUInt();
      if (!this.ConnectionTick.IsValid)
        this.ConnectionTick = simulationTick;
      if (this.Engine.Config.EnableInterestManagement)
      {
        clientSimulation.ReceivedGroups.Clear();
        uint num5 = reader.ReadUInt4();
        int num6 = 0;
        for (int index1 = 0; (long) index1 < (long) num5; ++index1)
        {
          int index2 = reader.ReadInt() + num6;
          clientSimulation.ReceivedGroups.Add(this.Engine._IM.AllGroups[index2]);
          num6 = index2;
        }
      }
      bool didWriteAll2 = reader.Read(1) == 1U;
      uint writtenGroupCount = reader.Read(20);
      int num7 = Math.Abs(this.DCTick - tick);
      NetickAllocator basisSnapshot = (NetickAllocator) null;
      bool flag2 = false;
      clientSimulation.IsLastTickFull = false;
      clientSimulation.InputReceiveDeltaStd = (double) NetickUtils.GetFloat(&num2, 0.01f) / 1000.0;
      clientSimulation.InputReceiveDeltaAvg = (double) NetickUtils.GetFloat(&num3, 0.01f) / 1000.0;
      clientSimulation.LocalInputSourceIndex = num1;
      if (this.DCTick.IsValid && tick.IsValid)
      {
        if (num7 > this.Engine.SnapshotCutOff)
        {
          this.DCTick = Tick.InvalidTick;
        }
        else
        {
          flag2 = true;
          basisSnapshot = clientSimulation._snapshots[tick % clientSimulation._snapshots.Length];
        }
      }
      else if (!tick.IsValid)
        flag2 = true;
      if (tick.IsValid && !didWriteAll2)
      {
        this.DCTick = Tick.InvalidTick;
        flag2 = false;
      }
      if (flag1)
        clientSimulation.ReadMeta(reader, didWriteAll1);
      if (didWriteAll2 && !tick.IsValid)
      {
        clientSimulation.IsLastTickFull = true;
        this.LastReceivedFullSnapshotTick_State = snapshotTick;
        this.DCTick = simulationTick;
        clientSimulation.ReadGroups(reader, writtenGroupCount, didWriteAll2, simulationTick, (NetickAllocator) null);
        clientSimulation.OnFullSnapshot(simulationTick, simReady);
      }
      else if (didWriteAll2 && tick.IsValid && !this.DCTick.IsValid)
        this.DCTick = Tick.InvalidTick;
      else if (flag2)
      {
        if (!didWriteAll2 && this.DCTick.IsValid && tick.IsValid)
          this.DCTick = Tick.InvalidTick;
        else if (didWriteAll2 || !tick.IsValid)
          this.LastReceivedFullSnapshotTick_State = snapshotTick;
        if (didWriteAll1)
          clientSimulation.ReadGroups(reader, writtenGroupCount, didWriteAll2, simulationTick, basisSnapshot);
        if (didWriteAll2)
        {
          clientSimulation.IsLastTickFull = true;
          this.DCTick = simulationTick;
          clientSimulation.OnFullSnapshot(simulationTick, simReady);
        }
      }
      if (!clientSimulation.IsLastTickFull)
        clientSimulation.IsLastTickFullPrevious = false;
      this.Engine._timer.OnServerPacket();
      this.Engine._profiler.ServerTickTime.Update(1f / (float) num4);
    }
  }
}
